<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父标签id" prop="areaPid">
      <el-input v-model="dataForm.areaPid" placeholder="区域父标签id"></el-input>
    </el-form-item>
    <el-form-item label="区域名称" prop="areaName">
      <el-input v-model="dataForm.areaName" placeholder="区域名称"></el-input>
    </el-form-item>
    <el-form-item label="区域类型" prop="areaCtype">
      <el-input v-model="dataForm.areaCtype" placeholder="区域类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          areaId: 0,
          areaPid: '',
          areaName: '',
          areaCtype: '',
          addTime: '',
          updateTime: '',
          deleted: ''
        },
        dataRule: {
          areaPid: [
            { required: true, message: '区域父标签id不能为空', trigger: 'blur' }
          ],
          areaName: [
            { required: true, message: '区域名称不能为空', trigger: 'blur' }
          ],
          areaCtype: [
            { required: true, message: '区域类型不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '加入时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更改时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.areaId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.areaId) {
			this.$axios.get(`/area/info/${this.dataForm.areaId}`).then((data)=>{
				if (data && data.data.code === 0) {
					this.dataForm.areaPid = data.data.area.areaPid
					this.dataForm.areaName = data.data.area.areaName
					this.dataForm.areaCtype = data.data.area.areaCtype
					this.dataForm.addTime = data.data.area.addTime
					this.dataForm.updateTime = data.data.area.updateTime
					this.dataForm.deleted = data.data.area.deleted
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
			var params = {
				'areaId': this.dataForm.areaId || undefined,
				'areaPid': this.dataForm.areaPid,
				'areaName': this.dataForm.areaName,
				'areaCtype': this.dataForm.areaCtype
			}
			this.$axios.post(`/area/${!this.dataForm.areaId ? 'save' : 'update'}`, params).then((data)=>{
				if (data && data.data.code === 0) {
				  this.$message({
				    message: '操作成功',
				    type: 'success',
				    duration: 1500,
				    onClose: () => {
				      this.visible = false
				      this.$emit('refreshDataList')
				    }
				  })
				} else {
				  this.$message.error(data.data.msg)
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      }
    }
  }
</script>
